#include <iostream>
#include <vector>
using namespace std;

// 归并排序计数逆序对（最少相邻交换次数）
static long long merge_count(vector<long long>& a, vector<long long>& buf, int l, int r) {
    if (l >= r) return 0;
    int mid = l + ((r - l) >> 1);
    long long cnt = 0;
    cnt += merge_count(a, buf, l, mid);
    cnt += merge_count(a, buf, mid + 1, r);

    // 合并并统计
    int i = l, j = mid + 1, k = l;
    while (i <= mid && j <= r) {
        if (a[i] <= a[j]) {
            buf[k++] = a[i++];
        } else {
            buf[k++] = a[j++];
            // 右侧当前元素比左侧 i..mid 都小，贡献 (mid - i + 1)
            cnt += (mid - i + 1);
        }
    }
    while (i <= mid) buf[k++] = a[i++];
    while (j <= r)   buf[k++] = a[j++];

    for (int t = l; t <= r; ++t) a[t] = buf[t];
    return cnt;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    while (true) {
        int n = 0;
        if (!(cin >> n)) break;  // 输入结束
        if (n == 0) break;       // 按题意 0 结束

        vector<long long> a(n), buf(n);
        for (int i = 0; i < n; ++i) cin >> a[i];
        long long ans = merge_count(a, buf, 0, n - 1);
        cout << ans << '\n';
    }
    return 0;
}
